<?php
// +----------------------------------------------------------------------
// | UCToo [ Universal Convergence Technology ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2020 https://www.uctoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Patrick <contact@uctoo.com>
// +----------------------------------------------------------------------
namespace app\admin\model\wechatopen;

use think\Model;
use think\Cache;


class Wechatopen extends Model
{
    // 表名
    protected $name = 'wechatopen';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'token_overtime_text',
        'pre_code_overtime_text'
    ];

    //自定义初始化
    protected static function init()
    {
        $wechatopen = self::get(['status' => 1]);  //读取第三方平台信息表中第一条状态为1的数据
        $config = [
            'app_id'   => $wechatopen->appid,
            'secret'   => $wechatopen->appsecret,
            'token'    => $wechatopen->token,
            'aes_key'  => $wechatopen->encodingAesKey,
            'oauth' => [
                'scopes'  =>['snsapi_login'],
                'callback'  => 'admin/index/wxlogin_callback',
            ]
        ];
        $wechat_default = config('wechat.default') ? config('wechat.default') : [];
        //合并wechat默认配置文件
        $config = array_merge($config, $wechat_default);
        Cache::set('wechatopen_config',$config);            //第三方平台参数加入缓存，后续初始化WeChatSDK等可以从缓存获取配置
    }

    public function getTokenOvertimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['token_overtime']) ? $data['token_overtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPreCodeOvertimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pre_code_overtime']) ? $data['pre_code_overtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setTokenOvertimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPreCodeOvertimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
